package com.infodms.dms.actions.claim.oldPart;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.bean.ClaimOldPartRemainStoreListBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.getCompanyId.GetOemcompanyId;
import com.infodms.dms.dao.claim.oldPart.ClaimOldPartStorageManagerDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.util.CommonUtils;
import com.infodms.dms.util.csv.CsvWriterUtil;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.mvc.context.ResponseWrapper;
import com.infoservice.po3.bean.PageResult;

/**
 * 类说明：索赔旧件管理--索赔旧件库存查询
 * 作者：  赵伦达
 */
public class ClaimOldPartStorageManager {
	
	public Logger logger = Logger.getLogger(ClaimOldPartStorageManager.class);
	private ActionContext act = null;
	private AclUserBean logonUserBean = null;
	private RequestWrapper request=null;
	private ClaimOldPartStorageManagerDao dao=null;
	private ResponseWrapper response =null;
	
	//url导向
	private final String queryOldPartStoreUrl = "/jsp/claim/oldPart/queryOldPartStoreList.jsp";
	/**
	 * Function：索赔旧件库存查询--初始化
	 * @param  ：	
	 * @return:		 
	 * @throw：	
	 * LastUpdate：	2010-6-18
	 */
	public void queryListPage(){
		try {
			act=ActionContext.getContext();
			logonUserBean=(AclUserBean) act.getSession().get(Constant.LOGON_USER);
			
			//取得该用户拥有的产地权限
			String yieldly = CommonUtils.findYieldlyByPoseId(logonUserBean.getPoseId());
			
			act.setOutData("yieldly", yieldly);
			act.setForword(queryOldPartStoreUrl);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔旧件库存查询--初始化");
			logger.error(logonUserBean, e1);
			act.setException(e1);
		}
	}
	/**
	 * Function：索赔旧件库存查询--条件查询
	 * @param  ：	
	 * @return:		 
	 * @throw：	
	 * LastUpdate：	2010-6-18
	 */
	@SuppressWarnings("unchecked")
	public void queryCurStoreList(){
		try {
			act=ActionContext.getContext();
			request = act.getRequest();
			logonUserBean=(AclUserBean) act.getSession().get(Constant.LOGON_USER);
			Long companyId=GetOemcompanyId.getOemCompanyId(logonUserBean);
			dao=ClaimOldPartStorageManagerDao.getInstance();
			
			Map params=new HashMap<String,String>();
			// 处理当前页
			Integer curPage = request.getParamValue("curPage") != null ? Integer
					.parseInt(request.getParamValue("curPage")) : 1;
			String supply_name=request.getParamValue("supply_name");
			String part_code=request.getParamValue("part_code");
			String part_name=request.getParamValue("part_name");
			String yieldly = request.getParamValue("yieldly");//产地
			String yieldlys = CommonUtils.findYieldlyByPoseId(logonUserBean.getPoseId());       //该用户拥有的产地权限
			
			params.put("company_id",companyId);
			params.put("supply_name", supply_name);
			params.put("part_code", part_code);
			params.put("part_name", part_name);//ClaimOldPartRemainStoreListBean
			params.put("yieldly", yieldly);
			params.put("yieldlys", yieldlys);
			
			PageResult<ClaimOldPartRemainStoreListBean> ps=dao.getCurStoreList(params, curPage, Constant.PAGE_SIZE);
			
			act.setOutData("ps", ps);
			act.setForword(queryOldPartStoreUrl);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔旧件库存查询--条件查询");
			logger.error(logonUserBean, e1);
			act.setException(e1);
		}
		
	}
	/**
	 * Function：索赔旧件库存查询--导出功能
	 * @param  ：	
	 * @return:		 
	 * @throw：	
	 * LastUpdate：	2010-7-12
	 */
	@SuppressWarnings("unchecked")
	public void toExcel(){
		OutputStream os = null;
		try {
			act=ActionContext.getContext();
			request = act.getRequest();
			logonUserBean=(AclUserBean) act.getSession().get(Constant.LOGON_USER);
			Long companyId=GetOemcompanyId.getOemCompanyId(logonUserBean);
			dao=ClaimOldPartStorageManagerDao.getInstance();
			response=act.getResponse();
			
			Map params=new HashMap<String,String>();

			
			String supply_name=request.getParamValue("supply_name");
			String part_code=request.getParamValue("part_code");
			String part_name=request.getParamValue("part_name");
			params.put("company_id",companyId);
			params.put("supply_name", supply_name);
			params.put("part_code", part_code);
			params.put("part_name", part_name);//ClaimOldPartRemainStoreListBean
			// 导出的文件名
			String fileName = "索赔旧件库存.csv";
			// 导出的文字编码
			fileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
			response.setContentType("Application/text/csv");
			response.addHeader("Content-Disposition", "attachment;filename="
					+ fileName);
			List<List<Object>> list = new LinkedList<List<Object>>();
			//表头
			List<Object> titleList = new LinkedList<Object>();
			titleList.add("序号");
			titleList.add("供应商名称");
			titleList.add("配件代码");
			titleList.add("配件名称");
			titleList.add("库存数");
			list.add(titleList);
			List<Map<String, Object>> results = dao.getCurStoreTotalExportList(params);
			for (int i = 0; i < results.size(); i++) {
				Map<String, Object> record = results.get(i);
				List<Object> dataList = new LinkedList<Object>();
				dataList.add(i+1);
				dataList.add(CommonUtils.checkNull(record.get("SUPPLY_NAME")));
				dataList.add(CommonUtils.checkNull(record.get("PART_CODE")));
				dataList.add(CommonUtils.checkNull(record.get("PART_NAME")));
				dataList.add(CommonUtils.checkNull(record.get("REMAIN_AMOUNT")));
				list.add(dataList);
			}
			os = response.getOutputStream();
			CsvWriterUtil.writeCsv(list, os);
			os.flush();
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔旧件库存查询--导出功能");
			logger.error(logonUserBean, e1);
			act.setException(e1);
		} finally {
			if (null != os) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
